package com.langll.ai.stock.controller;

import com.langll.ai.stock.model.StockDailyData;
import com.langll.ai.stock.service.IStockDailyDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 股票日线数据 控制器
 * </p>
 *
 * @author langll
 * @since 2025-04-21
 */
@Slf4j
@RestController
@RequestMapping("/stock/daily")
@RequiredArgsConstructor
@Tag(name = "股票日线数据接口", description = "股票日线数据管理相关接口")
public class StockDailyDataController {

    private final IStockDailyDataService stockDailyDataService;

    @Operation(summary = "添加股票日线数据")
    @PostMapping("/add")
    public ResponseEntity<Integer> addStockData(@RequestBody StockDailyData stockDailyData) {
        Assert.notNull(stockDailyData, "股票日线数据不能为空");
        Assert.notNull(stockDailyData.getStockCode(), "股票代码不能为空");
        Assert.notNull(stockDailyData.getTradeDate(), "交易日期不能为空");
        
        int result = stockDailyDataService.insert(stockDailyData);
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "批量添加股票日线数据")
    @PostMapping("/batch-add")
    public ResponseEntity<Integer> batchAddStockData(@RequestBody List<StockDailyData> stockDailyDataList) {
        Assert.notEmpty(stockDailyDataList, "股票日线数据列表不能为空");
        
        int result = stockDailyDataService.batchSave(stockDailyDataList);
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "根据ID查询股票日线数据")
    @GetMapping("/{id}")
    public ResponseEntity<StockDailyData> getStockDataById(@PathVariable Long id) {
        Assert.notNull(id, "ID不能为空");
        
        StockDailyData stockDailyData = stockDailyDataService.selectById(id);
        return ResponseEntity.ok(stockDailyData);
    }

    @Operation(summary = "根据股票代码获取最新日线数据")
    @GetMapping("/latest")
    public ResponseEntity<StockDailyData> getLatestStockData(@RequestParam String stockCode) {
        Assert.hasText(stockCode, "股票代码不能为空");
        
        StockDailyData stockDailyData = stockDailyDataService.getLatestStockData(stockCode);
        return ResponseEntity.ok(stockDailyData);
    }

    @Operation(summary = "根据股票代码和日期范围查询日线数据")
    @GetMapping("/list")
    public ResponseEntity<List<StockDailyData>> getStockDataList(
            @RequestParam String stockCode,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        Assert.hasText(stockCode, "股票代码不能为空");
        Assert.notNull(startDate, "开始日期不能为空");
        Assert.notNull(endDate, "结束日期不能为空");
        
        List<StockDailyData> stockDailyDataList = stockDailyDataService.getStockDataByDateRange(stockCode, startDate, endDate);
        return ResponseEntity.ok(stockDailyDataList);
    }

    @Operation(summary = "更新股票日线数据")
    @PutMapping("/update")
    public ResponseEntity<Integer> updateStockData(@RequestBody StockDailyData stockDailyData) {
        Assert.notNull(stockDailyData, "股票日线数据不能为空");
        Assert.notNull(stockDailyData.getId(), "股票日线数据ID不能为空");
        
        int result = stockDailyDataService.updateById(stockDailyData);
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "删除股票日线数据")
    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> deleteStockData(@PathVariable Long id) {
        Assert.notNull(id, "ID不能为空");
        
        int result = stockDailyDataService.deleteById(id);
        return ResponseEntity.ok(result);
    }
}